<template>
  <section class="common-container">
    <div v-if="!otherConfig.showRightBtn" class="carousel-header">
      <h2 class="title">{{ title }}</h2>
    </div>
    <div v-else class="carousel-header go-flex-sc">
      <h2 class="title">{{ title }}</h2>
      <h2 class="button" @click="handleClearHistory">{{ otherConfig.buttonText }}</h2>
    </div>
    <swiper class="swiper-container" :slides-per-view="slidesPerView" :space-between="spaceBetween" @swiper="onSwiper"
      @slideChange="onSlideChange" :modules="modules" :pagination="pagination" :autoplay="autoplay"
      :navigation="navigation" :loop="loop">
      <swiper-slide class="swiper-item" v-for="(item, index) in options" :key="index">
        <a :href="`/product/${item.url}.html`" target="_self" :title="item.name">
          <img class="item" v-if="item.detail_images" :src="item.detail_images?.[0]"/>
          <div class="item-info">
            <div class="item-name" v-if="otherConfig.showTitle">{{ item.name }}</div>
            <div class="item-description" v-if="otherConfig.showDescription">{{ item.description }}</div>
            <div class="item-price" v-if="otherConfig.showPrice"
              :style="{ textAlign: !otherConfig.showRightBtn ? 'left' : 'center' }">{{ useCurrency.currentCurrency.unit }}{{ proxy.$ConvertPrice(item.min_price)}} </div>
          </div>
        </a>
      </swiper-slide>
    </swiper>
  </section>

</template>
<script lang="ts" setup>
import { ref,getCurrentInstance } from 'vue';
import { Swiper, SwiperSlide } from 'swiper/vue';
import { Navigation, Pagination, A11y, Autoplay } from 'swiper/modules';
import 'swiper/css';
import 'swiper/css/navigation';
import 'swiper/css/pagination';
import 'swiper/css/autoplay';
import { useRouter, useRoute } from 'vue-router';
import { useCurrencyStore } from '@/store';

const { proxy } = getCurrentInstance();
const useCurrency = useCurrencyStore();
const router = useRouter();
const props = defineProps({
  //轮播列表
  options: {
    type: Array,
    default: () => ([])
  },
  otherConfig: {
    type: Object,
    default: () => ({
      showTitle: true,// 是否显示标题
      showDescription: true, // 是否显示描述
      showPrice: true, // 是否显示价格
      showRightBtn: false, // 是否显示右侧按钮
      buttonText: 'CLEAR' // 右侧按钮文本
    })
  },
  // 轮播一行显示图片个数
  slidesPerView: {
    type: Number,
    default: 3
  },
  // 轮播间隔
  spaceBetween: {
    type: Number,
    default: 20
  },
  // 自动播放
  autoplay: {
    type: Boolean,
    default: true
  },
  // 自动播放间隔
  autoplayDelay: {
    type: Number,
    default: 2000
  },
  // 是否循环播放
  loop: {
    type: Boolean,
    default: true
  },
  // 是否显示分页器
  pagination: {
    type: Boolean,
    default: true
  },
  // 是否显示导航按钮
  navigation: {
    type: Boolean,
    default: true
  },
  // 标题
  title: {
    type: String,
    default: 'TITLE'
  }
});

const emits = defineEmits(['handleCarouselItemClick', 'handleClearHistory'])

const modules = [Navigation, Pagination, A11y, Autoplay]
const swiperInstance = ref<typeof Swiper | null>(null);

const onSwiper = (swiper: typeof Swiper) => {
  swiperInstance.value = swiper;
};
const onSlideChange = () => {
  console.log('slide change');
};

const handleClick = (item: any) => {
  // router.push({
  //   path: '/details',
  //   query: { id: item.id }
  // });
  window.location.href = `/product/${item.url}.html`
  // console.log(item,"09")
  // emits('handleCarouselItemClick', item)
}

const handleClearHistory = () => {
  emits('handleClearHistory')
}

</script>

<style lang="scss" scoped>
.swiper-container {
  background-color: transparent;
  position: relative;
}

.common-container {
  padding: 60px 0;

  .carousel-header {
    margin-bottom: 24px;

    .title {
      font-size: 16px;
      font-weight: bold;
      letter-spacing: 0.5px;
      margin: 0;
    }

    .button {
      font-size: 12px;
      color: #000;
      background: none;
      border: none;
      padding: 0;
      cursor: pointer;
      text-decoration: underline;
      font-weight: bold;
    }
  }
}

.item {
  width: 100%;
  height: auto;
}

.swiper-item {
  margin-right: 20px;
  text-align: left;

  &:last-child {
    margin-right: 0;
  }

  img {
    width: 100%;
    height: 100%;
    aspect-ratio: 3/4;
    object-fit: cover;
    margin-bottom: 12px;
  }
}

.item-info {
  .item-name {
    font-size: 12px;
    font-weight: 600;
    margin-bottom: 4px;
    text-transform: uppercase;
  }

  .item-description {
    font-size: 12px;
    color: #666;
    margin-bottom: 4px;
    line-height: 1.4;
  }

  .item-price {
    font-size: 12px;
    font-weight: 600;
  }
}

::v-deep(.swiper-container .swiper-button-prev, .swiper-rtl .swiper-button-next),
::v-deep(.swiper-container .swiper-button-next, .swiper-rtl .swiper-button-prev) {
  color: #000 !important;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  line-height: 36px;
  text-align: center;
  font-size: 12px;
  top: calc(50% - 12px);
  transform: translateY(-50%);

  &:after {
    font-size: 12px !important;
    animation: swiper-preloader 1s infinite ease-in-out;
  }
}

::v-deep(.swiper-container .swiper-button-next, .swiper-rtl .swiper-button-prev) {
  right: 0;
}

::v-deep(.swiper-container .swiper-button-prev, .swiper-rtl .swiper-button-next) {
  left: 0;
}
</style>
